﻿using System; 
using System.Data;
using System.Web;

namespace msBsc.WebUi
{
    public partial class wfBseDataSeekSetFinder : msBsc.Cls.BPageHt
    { 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls(); 
            }
        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
                //DataSet dsCons = this.DataConst;
                // BoundComboBox("VALUE_TYPE", dsCons, "CONSTANT_DISP_TEXT", "CONSTANT_VALUE", "CONSTANT_CATEGORY_CID='EPM_STATUS_VALUE_TYPE_CID'", "SEQ_NUM ASC", true);
                  
            }
        }
        //重构基类的回调处理函数，以strAction属性来判断是什么类型的回调，该变量的值对应前台的ACTION参数，如：按钮的名称等
        public override void InitialByParam()
        {
            if (this.gtGridRet != null)
            {
                if (this.gtGridRet.action == "load" || this.gtGridRet.action == "export")//处理GRID的数据原获取和数据导出
                {
                    DataSet ds = GetDisplayData(this.GetParamHtValue("gtGridID"), this.strAction, this.sCurrPageRadom);
                    if (ds != null && ds.Tables.Count > 0)
                        SetReturnDatatable(ds.Tables[0], "SEEK_SET_ID");//为表的KEY字段，可填可不填
                }
                //处理数据保存
                if (this.gtGridRet.action == "save" && (gtGridRet.insertedRecords.Length > 0 || gtGridRet.updatedRecords.Length > 0 || gtGridRet.deletedRecords.Length > 0))
                {
                    DataSet ds = GetDisplayData(this.GetParamHtValue("gtGridID"), this.strAction, this.sCurrPageRadom);
                    try
                    {
                        if (ds != null && ds.Tables.Count > 0)
                        {
                            
                            //处理记录新增，前台已传入新增的ROW
                            for (int i = 0; i < gtGridRet.insertedRecords.Length; i++)
                            {
                                DataRow drNew = ds.Tables[0].NewRow();
                                msBsc.AppLogic.Cfg.HttpTools.CopyHashToDataRow(gtGridRet.insertedRecords[i], drNew);
                                drNew["SEEK_SET_ID"] = new Epm.AppLogic.Framework.CDbAccess().NewID();
                                if (drNew["SEEK_NO"].ToString() == string.Empty)
                                    drNew["SEEK_NO"] = msBsc.SvRef.ApplicationConst.Const.GetNewSerialNo("SEEK_NO_SET", "CN", 4);
                                drNew["REC_ACTIVE"] =drNew["REC_ACTIVE"].ToString()=="True";
                                ds.Tables[0].Rows.Add(drNew);
                            }

                            //处理记录更新，前台已传入更新的ROW
                            for (int i = 0; i < gtGridRet.updatedRecords.Length; i++)
                            {
                                string stridtm = gtGridRet.updatedRecords[i]["SEEK_SET_ID"].ToString();
                                if (stridtm != string.Empty)
                                {
                                    DataRow[] arrtemr = ds.Tables[0].Select("SEEK_SET_ID='" + stridtm + "'");
                                    if (arrtemr.Length > 0)
                                    {
                                        msBsc.AppLogic.Cfg.HttpTools.CopyHashToDataRow(gtGridRet.updatedRecords[i], arrtemr[0]);
                                    }
                                }
                            }

                            //处理记录删除，前台已传入删除的ROW
                            for (int i = 0; i < gtGridRet.deletedRecords.Length; i++)
                            {
                                string stridtm = gtGridRet.deletedRecords[i]["SEEK_SET_ID"].ToString();
                                if (stridtm != string.Empty)
                                {
                                    DataRow[] arrtemr = ds.Tables[0].Select("SEEK_SET_ID='" + stridtm + "'");
                                    if (arrtemr.Length > 0)
                                    {
                                        arrtemr[0].Delete();
                                    }
                                }
                            }
                            if (ds.HasChanges())
                            {
                                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                                {
                                    Epm.AppLogic.Framework.CBizObject objdata = new Epm.AppLogic.Framework.CBizObject();
                                    objdata.DataObj = new Epm.AppLogic.Framework.CDbAccess();
                                    objdata.DataObj.Init(ds.Tables[0]);
                                    objdata.DataObj.KeyField = objdata.DataObj.Fields["SEEK_SET_ID"];
                                    objdata.Update(ds);
                                    ds.AcceptChanges();

                                    ts.Complete();
                                    ts.Dispose();
                                   // msBsc.SvRef.ApplicationConst.Const.initdicSeekData();
                                    //Icss.AppLogic.Bse.ApplicationConst.Const.dsBseDataSeekSet = null; 

                                }
                            }
                        }
                    }
                    catch (Exception err)
                    {
                        ds.RejectChanges();
                        gtGridRetSaveException = err.Message;
                    }
                }
                if (gtGridRetSaveSuccess && (this.gtGridRet.action == "save" || strAction == "btnSave"))//根据传入的回调类型进行处理，这里响应编辑按钮
                {
                    //保存后将页面置为查看状态
                    SetMode("VIEW");
                }
            }
            else if (strAction == "btnEdit")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("EDIT");
            }
            else if (strAction == "btnNotSave")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("VIEW");
            }
            else if (strAction == "btnConfirm")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetItemStatus(this.GetParamHtValue("CHECK_ID"), "True");
                this.ExecJScript("btnDo('mFinder');");
            }
            else if (strAction == "btnCancel")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetItemStatus(this.GetParamHtValue("CHECK_ID"), "False");
                this.ExecJScript("btnDo('mFinder');");
            }

            base.InitialByParam();
        }
        private void SetItemStatus(string strIds, string strStatus)
        {
            Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess();
            DataSet ds = oCDbAccess.QueryBySQL("SELECT * FROM BSE_DATA_SEEK_SET WHERE SEEK_SET_ID IN ('" + strIds.Replace(",","','") + "')");// this.objBseConditionSet.SelectByIds(strIds);
            ds.Tables[0].TableName = "BSE_DATA_SEEK_SET";
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                if (ds.Tables[0].Rows[i]["REC_ACTIVE"].ToString() != strStatus)
                {
                    ds.Tables[0].Rows[i]["REC_ACTIVE"] = strStatus == "True";
                }
            }
            if (ds.HasChanges())
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    Epm.AppLogic.Framework.CBizObject objdata = new Epm.AppLogic.Framework.CBizObject();
                    objdata.DataObj = new Epm.AppLogic.Framework.CDbAccess();
                    objdata.DataObj.Init(ds.Tables[0]);
                    objdata.DataObj.KeyField = objdata.DataObj.Fields["SEEK_SET_ID"];
                    objdata.Update(ds);
                    ds.AcceptChanges();

                    ts.Complete();
                    ts.Dispose();
                    //msBsc.SvRef.ApplicationConst.Const.initdicSeekData();
                    //Icss.AppLogic.Bse.ApplicationConst.Const.dsBseDataSeekSet = null; 
                }
            }
        }

        //修改页面状态
        private void SetMode(string strMode)
        {
            EditMode = strMode;switch (strMode)
            {
                case "EDIT":
                    EnabledLinkBtn("btnEdit", false);
                    EnabledLinkBtn("btnSave", true);
                    EnabledLinkBtn("btnNotSave", true);
                    EnabledLinkBtn("btnConfirm", false);
                    EnabledLinkBtn("btnCancel", false);
                    this.ExecJScript("doEditGt('myGrid1');");
                    break;
                case "ADD":
                    EnabledLinkBtn("btnEdit", false);
                    EnabledLinkBtn("btnSave", true);
                    EnabledLinkBtn("btnNotSave", false);
                    EnabledLinkBtn("btnConfirm", false);
                    EnabledLinkBtn("btnCancel", false);
                    this.ExecJScript("doEditGt('myGrid1');");
                    break;
                case "VIEW":
                    EnabledLinkBtn("btnEdit", true);
                    EnabledLinkBtn("btnSave", false);
                    EnabledLinkBtn("btnNotSave", false);
                    EnabledLinkBtn("btnConfirm", true);
                    EnabledLinkBtn("btnCancel", true);
                    this.ExecJScript("doCancelGt('myGrid1');");
                    break;
            }
        }
        //获取数据原,"DP_DATA"字符一定不要改变，否则系统不能自动清除缓存
        public DataSet GetDisplayData(string strgtGridID, string strSrcF, string strsRadom)
        {
            if (strgtGridID == "myGrid1")
            {
                DataSet ds;
                if (strSrcF == "mFinder" || !this.SessionIsExists(strsRadom + "DP_DATA", "1"))//点击查询按钮时，或需要数据而没有缓存时，根据条件取出数据记录
                {
                    string strStatus = this.GetParamHtValue("txtStatus");
                    Epm.AppLogic.Framework.CDbAccess oCDbAccess = new Epm.AppLogic.Framework.CDbAccess();
                    ds = oCDbAccess.QueryBySQL("SELECT * FROM BSE_DATA_SEEK_SET WHERE '" + strStatus + "'='' OR  '" + strStatus + "'<>'' AND REC_ACTIVE= '" + strStatus + "'");// this.objBseConditionSet.SelectByIds(strIds);
                    ds.Tables[0].TableName = "BSE_DATA_SEEK_SET";
                    //ds = this.objBseConditionSet.SelectConditionSetForHandle(this.GetParamHtValue("CONDITION_TYPE_CID"), this.GetParamHtValue("TABLE_CDE"), strStatus);
                    ds.AcceptChanges();
                    SetSession(strsRadom + "DP_DATA", ds, "1");
                    //查询时，将页面置为查看状态
                    if (strSrcF == "mFinder")
                        SetMode("VIEW");
                }
                else
                {
                    ds = (DataSet)this.GetSession(strsRadom + "DP_DATA", "1");
                }
                return ds;
            }
            else
            {
                return null;
            }
        }

    }
}

